package lrz.shiftplanner.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.util.LinkedList;

import javax.swing.*;
import javax.swing.border.LineBorder;

import lrz.shiftplanner.application.MainWindow;
import lrz.shiftplanner.staff.Person;


public class NamePanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1218385728827451228L;
	private Person person;
	private JLabel l;
	private JButton button;
	private JProgressBar bar;
	//private MainWindow mainWindow;
	private static JPopupMenu popUp;
	private static JCheckBoxMenuItem m2;
	public LinkedList<GuiListener> guiListeners = new LinkedList<GuiListener>();
	
	private static void initPopup() {
		if (popUp == null) {
			popUp = new JPopupMenu();
			JMenuItem m1 = new JMenuItem("Aussetzen");
			m2 = new JCheckBoxMenuItem("Fertig");
			

			ActionListener al = new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					JMenuItem item = (JMenuItem) e.getSource();
					NamePanel p = (NamePanel) ((JPopupMenu) (item).getParent())
							.getInvoker();
					
					if (item.getText().equals("Aussetzen")) {
						p.invokeWaitRound();
					}
					
					if (item.getText().equals("Fertig")) {
						p.setFinished(((JCheckBoxMenuItem) (item)).getState());
					}
				}

			};

			m1.addActionListener(al);
			m2.addActionListener(al);

			popUp.add(m1);
			popUp.add(m2);
		}
	}
	
	public void showPopup(boolean checked) {
		if (MainWindow.updateMode)
			return;
		m2.setSelected(checked);
		popUp.show(this, 10, 10);
	}
	
	
	public NamePanel(Person person) {
		this.person = person;
		setBorder(new LineBorder(Color.GRAY));
		
		initPopup();
		
		setLayout(new BorderLayout());
		l = new JLabel(person.getName());
		l.setOpaque(false);
		
		l.setFont(AdaptFontFactory.registerFont(l,AdaptFontFactory.SMALL));
		
		
		bar = new JProgressBar();
		bar.setMaximum(person.getCapacity()*5);
		//bar.setBounds(0,0, 10, 20);
		bar.setStringPainted(true);
		
		button = new JButton("OK");
		add(button, BorderLayout.EAST);
		
		JPanel barPanel = new JPanel();
		barPanel.setLayout(new FlowLayout(FlowLayout.CENTER,4,4));
		barPanel.add(bar);
		add(barPanel, BorderLayout.SOUTH);
		
		
		add(l,BorderLayout.CENTER);
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				invokeNextPerson();			
			}
			
		});
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getButton()== MouseEvent.BUTTON3)
					showPopup(getPerson().finished);
			}
		});
		refreshAppearance();
	}
	
	public void invokeNextPerson() {
		for (GuiListener l : guiListeners)
			l.actionPerformed(GuiEvent.NamePanelChanged, this);
		
	}
	

	public void invokeWaitRound() {
		person.waitRound();
		invokeNextPerson();
		System.out.println("Aussetzen");
	}
	
	public void setFinished(boolean finished) {
		person.finished = finished;
		setBackground(finished?Colors.cCorrectBG:Colors.cHolidayBG);
	}
	
	public Person getPerson() {
		return person;
	}
	
	
	public void setActive(boolean active) {
		setBackground(active?Colors.cMarkedBG:person.finished?Colors.cCorrectBG:Colors.cHolidayBG);
		button.setVisible(active);
		validate();
	}
	
	public void refreshAppearance() {
		bar.setValue(person.getShiftNumber()*5+person.getStandby());
		
		DecimalFormat df = new DecimalFormat("0.0");

		/*
		bar.setString(person.getName()+ " ("+df.format(person.getShiftNumber()+
				person.getStandby()*0.2f)+" / "+person.getCapacity()+")");
				*/
		bar.setString(df.format(person.getShiftNumber()+
				person.getStandby()*0.2f)+" / "+person.getCapacity());
	}
}
